package com.bank.trade_records.cun_money.service.impl;

import com.bank.trade_records.cun_money.dao.CunMoneyDao;
import com.bank.trade_records.cun_money.entity.CunMoney;
import com.bank.trade_records.cun_money.service.CunMoneyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CunMoneyServiceImpl implements CunMoneyService {

    @Autowired
    private CunMoneyDao cunMoneyDao;

    @Override
    public CunMoney getCunMoneyById(Long id) {
        return cunMoneyDao.selectById(id);
    }

    @Override
    @Transactional
    public boolean addCunMoney(CunMoney CunMoney) {
        return cunMoneyDao.insert(CunMoney) > 0;
    }

    @Override
    @Transactional
    public boolean updateCunMoney(CunMoney CunMoney) {
        return cunMoneyDao.updateById(CunMoney) > 0;
    }

    @Override
    @Transactional
    public boolean deleteCunMoneyById(Long id) {
        return cunMoneyDao.deleteById(id) > 0;
    }

    @Override
    public List<CunMoney> getAllCunMoneys() {
        return cunMoneyDao.selectList(null);
    }

    @Override
    @Transactional
    public boolean batchInsert(List<CunMoney> cunMoneyList) {
        return cunMoneyDao.insertBatchSomeColumn(cunMoneyList) > 0; // MyBatis-Plus提供了批量插入的方法
    }

    @Override
    public Page<CunMoney> getCunMoneyPage(Page<CunMoney> CunMoneyPage) {
        return cunMoneyDao.selectPage(CunMoneyPage, null);
    }
    public List<CunMoney> getCunMoneyListByAccountId(long accountId){
        QueryWrapper<CunMoney> cunMoneyQueryWrapper=new QueryWrapper<>();
        cunMoneyQueryWrapper.eq("account_id", accountId);
        return cunMoneyDao.selectList(cunMoneyQueryWrapper);
    }

    public List<Object> getDailyTransactionAmounts() {
        List<Map<String, Object>> daoResult = cunMoneyDao.getDailyTransactionAmounts();
        System.out.println(daoResult);
        List<Object> totalAmounts = new ArrayList<>();
        totalAmounts.add("存款记录");
        for (Map<String, Object> map : daoResult) {
            Double totalAmount = (Double) map.get("total_amount");
            totalAmounts.add(totalAmount);
        }

        return totalAmounts;
    }
}



